package com.cloud.importBean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.cglib.proxy.Proxy;
import org.springframework.stereotype.Component;

@Component
public class RepositoryScanner implements BeanDefinitionRegistryPostProcessor {
    private MyFactory myFactory = new MyFactory();

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        Object o = Proxy.newProxyInstance(RepositoryScanner.class.getClassLoader(), new Class[]{Myinterface.class}, myFactory);
        GenericBeanDefinition definition = new GenericBeanDefinition();
        definition.getConstructorArgumentValues()
                .addGenericArgumentValue(myFactory);
        definition.setBeanClass(o.getClass());    //设置类
        definition.setScope("singleton");       //设置scope
        definition.setLazyInit(false);          //设置是否懒加载
        definition.setAutowireCandidate(true);  //设置是否可以被其他对象自动注入
        beanDefinitionRegistry.registerBeanDefinition("myinterfaceHelloBean", definition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}